<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
    <style>
        .container { width: 300px; margin: 100px auto; }
        input { margin: 5px 0; width: 100%; }
    </style>
</head>
<body>
<div class="container">
    <h2>用户登录</h2>
    <input type="text" id="number" placeholder="账号"><br>
    <input type="password" id="password" placeholder="密码"><br>
    <button onclick="login()">登录</button>
    <button onclick="location.href='register.html'">注册</button>
    <p id="message"></p>
</div>

<script>
    async function login() {
        const account = {
            number: document.getElementById('number').value.trim(), // 去除前后空格
            password: document.getElementById('password').value.trim()
        };

        try {
            const response = await fetch('/api/account/login', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify(account)
            });

            const result = await response.json();
            if (result.success) {
                // 确保账号正确存储
                sessionStorage.setItem('currentUser', account.number);
                console.log('当前用户:', account.number); // 调试输出
                window.location.href = 'homepage.html';
            } else {
                alert('登录失败: ' + result.message);
            }
        } catch (error) {
            console.error('登录错误:', error);
        }
    }
</script>
</body>
</html>